
/**
 *\*\file log.h
 *\*\author Nations
 *\*\version v1.0.1
 *\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 **/
#ifndef __BSP_4G_COMMON_H__
#define __BSP_4G_COMMON_H__

#include "bsp_4g_task.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "stdint.h"

#include "EC800M_DEF.h"

#include "http_connect.h"
#include "cw32l010_gpio.h"

#include "cw32l010_systick.h"

#define RT_NULL NULL
#define rt_malloc malloc
#define rt_strstr strstr
#define rt_memcpy memcpy
#define rt_strlen strlen

#define rt_enter_critical()
#define rt_exit_critical()

#define rt_thread_mdelay(x) SysTickDelay(x)
#define rt_kprintf(fmt, ...) printf(fmt, ##__VA_ARGS__)
#define rt_snprintf snprintf
#define rt_strnlen(x, y) strlen(x)

#define EC800E_MODEL_PWR_PORT CW_GPIOB
#define EC800E_MODEL_PWR_PIN GPIO_PIN_3

#define EC800E_MODEL_RST_PORT CW_GPIOB
#define EC800E_MODEL_RST_PIN GPIO_PIN_4

#define EC_Device_Power_High() EC800E_MODEL_PWR_PORT->BSRR = EC800E_MODEL_PWR_PIN
#define EC_Device_Power_Low() EC800E_MODEL_PWR_PORT->BRR = EC800E_MODEL_PWR_PIN

#define EC_Device_Reset_High() EC800E_MODEL_RST_PORT->BSRR = EC800E_MODEL_RST_PIN
#define EC_Device_Reset_Low() EC800E_MODEL_RST_PORT->BRR = EC800E_MODEL_RST_PIN

typedef enum
{
    DRV_4G_REC_IDLE = 0, // 节点空闲
    DRV_4G_REC_WRITING,  // 等待接收
    DRV_4G_REC_STANDBY,  // 接收完成
    DRV_4G_REC_READING,  // 处理中
} rec_4G_queue_status;

typedef struct rec_4G_unit
{
    struct rec_4G_unit *next;
    short length;
    short current;
    rec_4G_queue_status status;
    char *buf;
} rec_4G_unit;

typedef struct
{
    unsigned int wait_retun : 1;
    unsigned int net_sta : 1;
    unsigned int sim_sta : 1;
    unsigned int link_sta : 1;
    unsigned int connecting_sta : 1;
    unsigned int mqtt_data_sta : 1;
    unsigned int mqtt_restart_connect : 1;
    unsigned int factory_data_send : 2;

    rec_4G_unit *current_write;
    rec_4G_unit *current_read;

    unsigned short read_buffer_size;
    unsigned short send_buffer_size;
    unsigned short retytimes;
    unsigned short send_busy;
    unsigned short battery;
    struct
    {
        char imei[20];
        char qccid[50];
    } moudle;
    struct
    {
        float longitude;
        float latitude;
    } location;

    uint32_t CSQ_RSSI;
    uint8_t keepalive_pub_flag;

} uart_4G_para;

extern uart_4G_para *device_4g;

typedef enum
{
    DEVCIE_REBOOT = 0,
    DEVICE_AT_TEST,
    DEVICE_COM_ECHO_OFF,
    DEVICE_SET_TOKEN,
    DEVICE_GET_IMEI,
    DEVICE_GET_QCCID,
    DEVICE_GET_CVERSION,
    DEVICE_GET_QGMR,
    DEVICE_GET_CGMR,
    DEVCIE_CHECK_SIM,
    DEVICE_GET_CSQ,
    // DEVICE_APN_CFG_PDP,
    // DEVICE_APN_ACTIVATE_PDP,
    // DEVICE_APN_CHECK_IP,
    DEVICE_NET_CREG,
    DEVICE_CLOSE_DATA_ECH0,
    DEVICE_ENABLE_URC,
    DEVICE_SET_MAIN_UART,
    DEVCIE_GET_NET_STATE,
    DEVICE_CFG_TCP_KEEPALIVE,
    DEVCIE_GET_LOCATION,
    DEVICE_HTTP_CONNECT,
    DEVICE_HTTP_DISCONNECT,
    DEVICE_MQTT_CONNECT,
    DEVICE_DATA_TRANS,
    DEVICE_INIT_SUCCESS,
    DEVICE_TASK_STOP,
    DEVICE_TASK_FACTORY_STOP,
    DEVCIE_NET_DATA_LISTEN,
} _device_init_step_enum;

typedef enum
{
    HTTP_REQ_RESET = 0,
    HTTP_REQHEADER_ENALBE,
    HTTP_RSPHEADER_ENABLE,
    HTTP_CFG_URL_START,
    HTTP_CFG_URL_DATA,
    HTTP_REQ_GET_SATRT,
    HTTP_REQ_GET_DATA,
    HTTP_REQ_POST_START,
    HTTP_REQ_POST_DATA,
    HTTP_RESP_LISTEN,
    HTTP_RESP_DATA_READ,
    HTTP_RESP_READ,
    HTTP_PRAR_READ_SUCCESS,
    HTTP_PRAR_READ_FAILED,
    HTTP_PARA_FACTORY_TEST
} _server_connct_step_enum;

typedef enum
{
    MQTT_SET_RECMODE = 0,
    MQTT_SET_DATAMODE,
    MQTT_CLIENT_OPEN,
    MQTT_WAIT_OPEN,
    MQTT_SET_CONNECT,
    MQTT_WAIT_CONNECT,
    MQTT_SET_DISCONNECT,
    MQTT_WAIT_DISCONNECT,
    MQTT_SET_KEEPALIVE,
    MQTT_SET_SUBCRIBE_TOPIC,
    MQTT_WAIT_SUBCRIBE,
    MQTT_CONNECT_SUCCESS,
} _mqtt_connct_step_enum;

typedef enum
{
    MQTT_DATA_TRANS_IDLE = 0,
    MQTT_DATA_TRANS_SET,
    MQTT_DATA_TRANS_SEND,
    MQTT_DATA_TRANS_ERROR,
} _mqtt_data_trans_enum;

typedef enum
{
    MQTT_DATA_REC_IDLE = 0,
    MQTT_DATA_REC_DATA_IN,
} _mqtt_data_recive_enum;

typedef enum
{
    MQTT_OK = 0,
} _mqtt_datatrans_enum;

_server_connct_step_enum server_connect(void);
_mqtt_connct_step_enum mqtt_service(void);

int Device_4G_Task(unsigned short para);

rec_4G_unit *DATA_LISTEN(int TIMEOUT);
rec_4G_unit *SEND_CMD(char *cmd, int TIME);
rec_4G_unit *SEND_CMD_DIRECT(char *cmd, int TIME);

void usart_4g_init(void);
void Gpio_4g_Init(void);

int device_data_rec_length(void);
int devcie_data_rec_reset(void);
int mqtt_recive_urc_list_pop(void);
int driver_4G_data_send(void *buf, int length);
_mqtt_datatrans_enum mqtt_data_decode(void);
void device_data_rec_restart(void);
int DATA_TRIM(rec_4G_unit *unit);
#endif /* __LOG_H__ */
